package com.yxq.mdt.app.modules.bottomsheet;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.yxq.mdt.app.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author L
 * @time 2019-10-30
 * @describe 选择交友意向
 */
public class PersonalPurposeSheetDialogFragment extends BottomSheetDialogFragment {
    Unbinder unbinder;
    @BindView(R.id.dialog_tv_save)
    TextView dialogTvSave;
    @BindView(R.id.dialog_check_box_follow)
    CheckBox dialogCheckBoxFollow;
    @BindView(R.id.dialog_check_box_do_not_disturb)
    CheckBox dialogCheckBoxDoNotDisturb;
    @BindView(R.id.dialog_check_box_look_forward_to)
    CheckBox dialogCheckBoxLookForwardTo;
    String[] data = new String[]{"随缘", "勿扰", "期待"};
    String friendsIntention;

    public PersonalPurposeSheetDialogFragment(String friendsIntention) {
        this.friendsIntention = friendsIntention;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置背景透明，才能显示出layout中诸如圆角的布局，否则会有白色底（框）
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.personal_purpose_bottom_sheet_fragment_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        initViews();
        return view;
    }

    int selectPosition = -1;

    private void initViews() {
        if (friendsIntention != null) {
            switch (friendsIntention) {
                case "随缘":
                    dialogCheckBoxFollow.setChecked(true);
                    break;
                case "勿扰":
                    dialogCheckBoxDoNotDisturb.setChecked(true);
                    break;
                case "期待":
                    dialogCheckBoxLookForwardTo.setChecked(true);
                    break;
            }
        }
        dialogCheckBoxFollow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                selectPosition = 0;
                if (!b) {
                    return;
                }
                dialogCheckBoxDoNotDisturb.setChecked(!b);
                dialogCheckBoxLookForwardTo.setChecked(!b);
            }
        });
        dialogCheckBoxDoNotDisturb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                selectPosition = 1;
                if (!b) {
                    return;
                }
                dialogCheckBoxFollow.setChecked(!b);
                dialogCheckBoxLookForwardTo.setChecked(!b);
            }
        });
        dialogCheckBoxLookForwardTo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                selectPosition = 2;
                if (!b) {
                    return;
                }
                dialogCheckBoxFollow.setChecked(!b);
                dialogCheckBoxDoNotDisturb.setChecked(!b);
            }
        });
    }


    PersonalPurposeSheetDialogFragmentInterface personalPurposeSheetDialogFragmentInterface;

    public void setPersonalPurposeSheetDialogFragmentInterface(PersonalPurposeSheetDialogFragmentInterface personalPurposeSheetDialogFragmentInterface) {
        this.personalPurposeSheetDialogFragmentInterface = personalPurposeSheetDialogFragmentInterface;
    }

    public interface PersonalPurposeSheetDialogFragmentInterface {
        void checked(int index, String data);
    }

    @OnClick(R.id.dialog_tv_save)
    public void onViewClicked() {
        dismiss();
        if (personalPurposeSheetDialogFragmentInterface != null) {
            personalPurposeSheetDialogFragmentInterface.checked(selectPosition, selectPosition != -1 ? data[selectPosition] : "");
        }
    }
}
